package ch.ethz.fcl.tools;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import org.opencv.imgproc.Imgproc;

import ch.ethz.fcl.Constant;
import ch.ethz.fcl.KinectContainer;
import ch.ethz.fcl.input.TableDepthMap;

public class KeyInteraction implements KeyListener {
	private static int x = 546;
	private static int y = 459;

	private static final int RADIUS = 1;
	private static final Scalar COLOR = new Scalar(0, 0, 255 * Constant.COLOR_SCALE);

	private KinectContainer container;

	public KeyInteraction(KinectContainer container) {
		this.container = container;
	}

	public static Mat drawPoint(BufferedImage image) {
		Mat mat = ImageTools.imageToMat(image);

		return drawPoint(mat);
	}

	public static Mat drawPoint(Mat mat) {
		Mat mat8UC3 = new Mat();
		if (mat.channels() == 1)
			Imgproc.cvtColor(mat, mat8UC3, Imgproc.COLOR_GRAY2RGB);
		else
			mat8UC3 = mat;
		Core.circle(mat8UC3, new Point(x, y), RADIUS, COLOR);
		return mat8UC3;
	}

	@Override
	public void keyTyped(KeyEvent e) {

	}

	@Override
	public void keyPressed(KeyEvent e) {
		int move = 1;
		switch (e.getKeyCode()) {
		case KeyEvent.VK_LEFT:
			x -= move;
			break;

		case KeyEvent.VK_RIGHT:
			x += move;
			break;
		case KeyEvent.VK_UP:
			y -= move;
			break;

		case KeyEvent.VK_DOWN:
			y += move;
			break;
		}

		TableDepthMap depthMap = container.getTableDepthMap();
		short[][] currentDepth = depthMap.getHistDepthMap();
		short[][] groundDepth = depthMap.getGroundDepthMap();
		short[][] height = depthMap.getHeightMap();

		System.out.println("box depth at (" + x + "," + y + ") is "
				+ currentDepth[y][x]);
		System.out.println("table depth at (" + x + "," + y + ") is "
				+ groundDepth[y][x]);
		System.out.println("depth at (" + x + "," + y + ") is " + height[y][x]
				+ "\n");
	}

	@Override
	public void keyReleased(KeyEvent e) {

	}
}
